/**
 * @file Body_Health_Clcok_display.ino
 * @brief The display part of project Body Health Clock
 * @author achdu0000@163.com
 * @version 1.0.0
 * @date 2024/07/07
 *
 * This is the main entrance to the program in Arduino.
 *
 */

#include "Body_Health_Clock_display_include.h"

extern lv_obj_t *health_and_temperature_label;

/**
 * @var lcd_mode
 * mode 0: init
 * mode 1: junior part
 * mode 2: senior part
 * mode 3: snoring part
 * mode 4: test part
 */
int lcd_mode = 0;
int last_lcd_mode = 0;

unsigned int lastTime = 0;
sensor_data sensor_data_ptr;
bool is_sensor_begin = false;
void setup()
{
  Serial.begin(115200);

  // init sensor data
  sensor_data_ptr.body_temperature_val = 0;
  sensor_data_ptr.body_blood_press_h_val = 0;
  sensor_data_ptr.body_blood_press_l_val = 0;
  sensor_data_ptr.body_blood_oxygen_val = 0;
  sensor_data_ptr.body_heart_rate_val = 0;
  sensor_data_ptr.body_walk_step_val = 0;

  display_on_create();
}

void loop()
{
// only lcd mode 4 need to read serial data
  if (lcd_mode == 4)
  {
    lv_obj_set_style_text_color(health_and_temperature_label, lv_color_make(0, 255, 0), LV_PART_MAIN | LV_STATE_DEFAULT);
    if (!is_sensor_begin)
    {
      Serial.println("sensor begin");
    }
    if (Serial.available())
    {
      int data[5] = {0, 0, 0, 0, 0};

      // read serial data and printf
      String dataStr = Serial.readStringUntil('\n');

      bool tmp = false;
      String begin_command = "sensor open";
      for (int i = 0; i < begin_command.length(); i++)
      {
        if (begin_command[i] == dataStr[i])
        {
          if (i == begin_command.length() - 1)
          {
            tmp = true;
          }
          continue;
        }
        else
        {
          break;
        }
      }
      if (tmp)
      {
        is_sensor_begin = true;
      }
      if (is_sensor_begin)
      {
        // Serial.print("Received data: ");
        // Serial.println(dataStr);
        String subDataStr = "";
        int index = 0;

        // parse data
        for (int i = 0; i < dataStr.length(); i++)
        {
          if (dataStr[i] == '_' || i == dataStr.length() - 1)
          {
            if (i == dataStr.length() - 1)
            {
              subDataStr += dataStr[i];
            }
            data[index] = subDataStr.toInt();
            subDataStr = "";
            index++;
          }
          else
          {
            subDataStr += dataStr[i];
          }
        }

        // process data
        switch (data[0])
        {
        case 0:
          sensor_data_ptr.body_temperature_val = data[1];
          break;
        case 1:
          sensor_data_ptr.body_blood_press_h_val = data[1];
          sensor_data_ptr.body_blood_press_l_val = data[2];
          sensor_data_ptr.body_blood_oxygen_val = data[3];
          sensor_data_ptr.body_heart_rate_val = data[4];
          break;
        case 2:
          sensor_data_ptr.body_walk_step_val = data[1];
          break;
        default:
          break;
        }

        // printf data
        LV_LOG_INFO("Temperature: %d.%d", sensor_data_ptr.body_temperature_val / 10, sensor_data_ptr.body_temperature_val % 10);
        LV_LOG_INFO("Blood Pressure(hign): %d", sensor_data_ptr.body_blood_press_h_val);
        LV_LOG_INFO("Blood Pressure(low): %d", sensor_data_ptr.body_blood_press_l_val);
        LV_LOG_INFO("Blood Oxygen: %d", sensor_data_ptr.body_blood_oxygen_val);
        LV_LOG_INFO("Heart Rate: %d", sensor_data_ptr.body_heart_rate_val);
        LV_LOG_INFO("Walk Step: %d", sensor_data_ptr.body_walk_step_val);
        
        // update lvgl label text
        lv_label_set_text_fmt(health_and_temperature_label, "%d.%d\n%d/%d\n%d\n%d\n%d",
                              sensor_data_ptr.body_temperature_val / 10,
                              sensor_data_ptr.body_temperature_val % 10,
                              sensor_data_ptr.body_blood_press_h_val,
                              sensor_data_ptr.body_blood_press_l_val,
                              sensor_data_ptr.body_blood_oxygen_val,
                              sensor_data_ptr.body_heart_rate_val,
                              sensor_data_ptr.body_walk_step_val);

        LV_LOG_INFO("Data received and processed.");
      }
      else
      {
        unsigned int currentTime = millis();
        if (currentTime - lastTime >= 2000)
        {
          lastTime = currentTime;
          LV_LOG_INFO("No data received.");
        }
      }
    }
  }

  if (lcd_mode != 4) {
    if (is_sensor_begin) {
      is_sensor_begin = false;
      Serial.println("sensor close");
    }
    if (last_lcd_mode == 4) {
      lv_obj_set_style_text_color(health_and_temperature_label, lv_color_make(255, 0, 0), LV_PART_MAIN | LV_STATE_DEFAULT);
    }
    
  }
}
